package com.ruoyi.invoice.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.invoice.domain.vo.InvoiceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.invoice.domain.TaxItem;
import com.ruoyi.invoice.mapper.InvoiceMapper;
import com.ruoyi.invoice.domain.Invoice;
import com.ruoyi.invoice.service.IInvoiceService;

/**
 * 销项发票Service业务层处理
 * 
 * @author XU
 * @date 2025-03-13
 */
@Service
public class InvoiceServiceImpl implements IInvoiceService 
{
    @Autowired
    private InvoiceMapper invoiceMapper;

    /**
     * 查询销项发票
     * 
     * @param id 销项发票主键
     * @return 销项发票
     */
    @Override
    public Invoice selectInvoiceById(Long id)
    {
        return invoiceMapper.selectInvoiceById(id);
    }

    /**
     * 查询销项发票列表
     * 
     *  销项发票
     * @return 销项发票
     */
    @Override
    public List<InvoiceVO> selectInvoiceList(InvoiceVO invoiceVO)
    {
        return invoiceMapper.selectInvoiceList(invoiceVO);
    }

    /**
     * 新增销项发票
     * 
     * @param invoice 销项发票
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInvoice(Invoice invoice)
    {
        // 校验发票号码唯一性
        Invoice existInvoice = invoiceMapper.checkInvoiceNumberUnique(invoice.getInvoiceNumber());
        if (StringUtils.isNotNull(existInvoice))
        {
            throw new ServiceException("发票号码'" + invoice.getInvoiceNumber() + "'已存在");
        }

        int rows = invoiceMapper.insertInvoice(invoice);
        insertTaxItem(invoice);
        return rows;
    }


    /**
     * 修改销项发票
     * 
     * @param invoice 销项发票
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInvoice(Invoice invoice)
    {
        invoiceMapper.deleteTaxItemByProjectName(invoice.getId());
        insertTaxItem(invoice);
        return invoiceMapper.updateInvoice(invoice);
    }

    /**
     * 批量删除销项发票
     * 
     * @param ids 需要删除的销项发票主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInvoiceByIds(Long[] ids)
    {

       for (Long id : ids) {
            String receipt = invoiceMapper.checkVoucherStatusById(id);
            if ("1".equals(receipt)) {
                throw new ServiceException("发票已生成单据，不能删除");
            }
        }
        invoiceMapper.deleteTaxItemByProjectNames(ids);
        return invoiceMapper.deleteInvoiceByIds(ids);
    }

    /**
     * 删除销项发票信息
     * 
     * @param id 销项发票主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInvoiceById(Long id)
    {
      // 判断是否已生成单据
        String receipt = invoiceMapper.checkVoucherStatusById(id);
        if ("1".equals(receipt)) {
            throw new ServiceException("发票已生成单据，不能删除");
        }
        invoiceMapper.deleteTaxItemByProjectName(id);
        return invoiceMapper.deleteInvoiceById(id);
    }

    /**
     * 新增发票数据信息
     * 
     * @param invoice 销项发票对象
     */
    public void insertTaxItem(Invoice invoice)
    {
        List<TaxItem> taxItemList = invoice.getTaxItemList();
        Long id = invoice.getId();
        if (StringUtils.isNotNull(taxItemList))
        {
            List<TaxItem> list = new ArrayList<TaxItem>();
            for (TaxItem taxItem : taxItemList)
            {
                taxItem.setProjectName(String.valueOf(id));
                list.add(taxItem);
            }
            if (list.size() > 0)
            {
                invoiceMapper.batchTaxItem(list);
            }
        }
    }
}
